import { Injectable } from '@angular/core';
import { Response } from '@angular/http';
import * as API from "@api/index";
import { HttpService } from '@core/http/http.service';
// import { GoodsOrderRespVO, GoodsSkuOrderVO, MerchandiseVO } from '@interface/goods.model';
// import {SNSResult, CommonPageVo} from '@interface/interface.model';
import { ToolsService } from '@shared/service/tools/tools.service';
import { Observable } from 'rxjs';
// import {OrderDetailRespVO} from "@interface/order.model";
import {Router} from "@angular/router";
import {
  CommonPageVo, GoodsOrderRespVO, GoodsSkuOrderVO, MerchandiseVO, OrderDetailRespVO,
  SNSResult, TicketDetailVO
} from "@interface/index";
// import {SNSResult} from "@interface/interface/interface.model";
// import {GoodsOrderRespVO, GoodsSkuOrderVO, MerchandiseVO} from "@interface/goods/goods.model";
//
// import {OrderDetailRespVO} from "@interface/order/order.model";
// import {CommonPageVo} from "@interface/shared/shared.model";


@Injectable()
export class OrderService {
  /**
   * @description //1: 待支付，2: 已支付，3: 支付失败，4: 已完成，5: 已关闭，6: 退款中，7: 已退货，9：预下单成功 ，10：超时强制关闭;
   * @type {{9: string; 1: string; 2: string; 3: string; 4: string; 5: string; 6: string; 7: string}}
   */
  public  paymentStatus = {
    '9' : '待完善',
    '1' : '待付款',
    '2' : '已付款',
    '3' : '支付失败',
    '4' : '已完成',
    '5' : '已关闭',
    '6' : '退款中',
    '7' : '已退货',
    '10' : '已关闭',
  };

  constructor(
    private httpService: HttpService,
    private toolsService: ToolsService,
    private router: Router
  ) { }


  /**
   * @description 查询订单确认信息
   * @author chensk740
   * @date 2017-09-13
   * @param data
   * @returns {Observable<R>}
   */
  getConfirmInfo(data): Observable<SNSResult<GoodsOrderRespVO>> {
    const params = this.toolsService.param(data);
    const url = API.TO_CONFIRM_URL + '?' + params;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  /**
   * @description 提交订单
   * @author chensk740
   * @date 2017-09-13
   * @param data
   * @returns {Observable<R>}
   */
  postOrderInfo(data): Observable<SNSResult<GoodsOrderRespVO>> {
    const params = this.toolsService.param(data);
    const url = API.ORDER_INFO_URL;
    return this.httpService.postUrlencode(url, data)
      .map((res: Response) => res.json());
  }

  getOrderInfo(params): Observable<SNSResult<GoodsOrderRespVO>> {
    return this.httpService.get(API.ORDER_INFO_URL + `?orderNumber=${params.orderNumber}`)
      .map((res: Response) => res.json());
  }

  getGoodsSkuDetail(params): Observable<SNSResult<GoodsSkuOrderVO>> {
    const url = API.GOODS_SKU_DETAIL + "?" + this.toolsService.param(params);

    return this.httpService.get(url).map((res: Response) => res.json())
      .filter(item => item.result === '0' && item.data);
  }

  getGoodsTemplateInfo(params): Observable<SNSResult<MerchandiseVO>> {
    const url = API.GOOD_TEMPLATE_INFO_URL + "?" + this.toolsService.param(params);
    return this.httpService.get(url)
      .map((res: Response) => res.json());
  }

  // 我的订单列表
  getOrderList(data: Object): Observable<SNSResult<CommonPageVo<GoodsOrderRespVO>>> {
    const url = API.MY_ORDER_URL + '?' + this.toolsService.params(data);
    return this.httpService.get(url).map((res: Response) => res.json());
  }
  // 订单详情页面
  getOrdersDetail(orderId: string): Observable<SNSResult<OrderDetailRespVO>> {
    const url = API.ORDER_INFO_URL + '?orderNumber=' + orderId;
    return this.httpService.get(url).map((res:Response) => res.json());
  }
  /**
   * @author @chensk740
   * @description 众筹权益支持
   * @param status
   * @returns {any}
   */
  crowdSupport(data: Object) {
    const url = API.CROWD_SUPPORT;
    return this.httpService.postUrlencode(url, data).map((res:Response) => res.json());
  }


  /**
   * @description 返回各种支付状态
   * @param status
   * @returns {any}
   */
  handlePaymentStatus(status) {
    return status && this.paymentStatus[status];
  }

  /**
   * @author @chensk740
   * @date  2017.09.21
   * @description 下单
   * @description 获得商品模板信息接口url然后跳转
   * @param data
   */
  getGoodsTemplateUrl(data) {
    this.getGoodsTemplateInfo(data).subscribe((res) => {
      if (res.result === '0') {
        this.toolsService.hideLoading();
        if (res.data) {
          const url = res.data.goodsBizUrl;
          this.router.navigateByUrl(url);
        }else {
          this.toolsService.showToast('系统错误');
        }
      }
    });
  }



}
